/***************************************************
 * sym_hash_interface.c
 *
 * Created on: Mar 20, 2017
 * Author: zjjin@ccore.com
 ***************************************************/

#include "./INCLUDE/compate.h"
#include "./INCLUDE/pci_csec.h"
#include "./INCLUDE/desc.h"
#include "./INCLUDE/desc_constr.h"
#include "./INCLUDE/sym_perf.h"

u8 malg,mtype,mopt;
int mdatasize,mivsize,mkeysize;

char mstr[32];

u8 malg2,mtype2,mopt2;
int mdatasize2,mivsize2,mkeysize2;

const char snoop_sm4sm3enc_sm4_ctrl_data[0x105] = {
	0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x9d, 0x58, 0x26, 0x00, 0xf6, 0xf3, 0x29, 0x05, 0xb2, 0xa2, 0x04,
	0x2f, 0xe4, 0x28, 0x2d, 0xf4, 0xd6, 0x07, 0xec, 0x25, 0x79, 0x19, 0x78, 0x72, 0x86, 0x9b, 0x23,
	0xc7, 0xaf, 0x32, 0x17, 0x82, 0x61, 0x75, 0xe8, 0xc4, 0x2e, 0xf3, 0xdd, 0x71, 0x1f, 0xd1, 0x6a,
	0x8d, 0x2a, 0xa7, 0xe9, 0x3e, 0xd5, 0x77, 0x8c, 0xa2, 0x3e, 0xb3, 0xc4, 0xb3, 0x30, 0x79, 0x84,
	0xbd, 0x04, 0x6d, 0x4b, 0x71, 0xa7, 0x78, 0x90, 0xfd, 0x99, 0x86, 0x21, 0x6f, 0x17, 0x06, 0xbd,
	0xdf, 0x6e, 0xee, 0x06, 0x5a, 0x23, 0xb5, 0x1f, 0xed, 0x90, 0xf0, 0xd1, 0xfa, 0xb5, 0xee, 0x37,
	0x45, 0x0a, 0xec, 0xce, 0x18, 0x5d, 0x73, 0x9b, 0xcb, 0xf9, 0x1b, 0xa2, 0x35, 0xa9, 0x3c, 0x82,
	0x8d, 0x04, 0x1b, 0xa6, 0x4e, 0xdc, 0xa4, 0x16, 0xae, 0xea, 0x81, 0xf2, 0xb4, 0x17, 0x68, 0x21,
	0xb8, 0x6e, 0x32, 0x1a, 0xd9, 0x14, 0x2a, 0xa6, 0x50, 0x2b, 0x25, 0x7b, 0xf2, 0x2d, 0xbb, 0xa5,
	0xf3, 0x80, 0x94, 0xe1, 0x11, 0x17, 0x70, 0x62, 0x00, 0xe7, 0x86, 0x42, 0xa8, 0x13, 0xc0, 0xdf,
	0x00, 0x40, 0xb7, 0x72, 0xb5, 0x56, 0x54, 0x55, 0xac, 0xe2, 0xf6, 0x0b, 0x30, 0x7b, 0xe4, 0xef,
	0x07, 0xe2, 0x51, 0x5d, 0xcf, 0x7d, 0xe6, 0x7d, 0x53, 0xa1, 0x04, 0x25, 0x58, 0xfa, 0x14, 0xa7,
	0x12, 0xfb, 0x9a, 0x2b, 0x72, 0x35, 0xcb, 0x82, 0x51, 0x08, 0x4a, 0x72, 0xe9, 0xf6, 0x4d, 0x15,
	0x19, 0xe3, 0xbf, 0xf6, 0xb1, 0xd7, 0x44, 0x7d, 0x83, 0x71, 0xd0, 0x26, 0xa8, 0x49, 0x38, 0x88,
	0xaa, 0x85, 0x4b, 0xc6, 0x39, 0xf5, 0xad, 0x2d, 0xd3, 0x94, 0x5d, 0xcc, 0xe9, 0x38, 0x00, 0x68,
	0xf2, 0xfc, 0x6f, 0x03, 0xf5, 0x2b, 0x60, 0xcb, 0x4d, 0x48, 0x28, 0x21, 0x18, 0xbe, 0xf3, 0x85,
	0xb1, 0x55, 0xb2, 0x76, 0xfd,
};

const char snoop_sm4sm3enc_sm3_ctrl_data[32] = {
	0x20, 0x78, 0x6e, 0x19, 0x62, 0xe6, 0x83, 0xa1, 0x9b, 0xb3, 0x1c, 0x0f, 0x59, 0x31, 0xd7, 0x29,
	0x0d, 0x99, 0xaa, 0xb2, 0x4f, 0xe4, 0x12, 0x4d, 0x8b, 0xe6, 0x2c, 0x87, 0xf0, 0x31, 0x6d, 0xe7,
};

const char snoop_sm4sm3dec_sm4_ctrl_data[0x105] = {
	0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x45, 0xa0, 0xa9, 0x3b, 0x4c, 0x8f, 0xde, 0x96, 0xa6, 0x7c, 0x22,
	0x9e, 0xc5, 0xdd, 0x44, 0xe2, 0x45, 0xa0, 0xa9, 0x3b, 0x4c, 0x8f, 0xde, 0x96, 0xa6, 0x7c, 0x22,
	0x9e, 0xc5, 0xdd, 0x44, 0xe2, 0x45, 0xa0, 0xa9, 0x3b, 0x4c, 0x8f, 0xde, 0x96, 0xa6, 0x7c, 0x22,
	0x9e, 0xc5, 0xdd, 0x44, 0xe2, 0x45, 0xa0, 0xa9, 0x3b, 0x4c, 0x8f, 0xde, 0x96, 0xa6, 0x7c, 0x22, 
	0x9e, 0xc5, 0xdd, 0x44, 0xe2, 0x45, 0xa0, 0xa9, 0x3b, 0x4c, 0x8f, 0xde, 0x96, 0xa6, 0x7c, 0x22,
	0x9e, 0xc5, 0xdd, 0x44, 0xe2, 0x45, 0xa0, 0xa9, 0x3b, 0x4c, 0x8f, 0xde, 0x96, 0xa6, 0x7c, 0x22, 
	0x9e, 0xc5, 0xdd, 0x44, 0xe2, 0x45, 0xa0, 0xa9, 0x3b, 0x4c, 0x8f, 0xde, 0x96, 0xa6, 0x7c, 0x22,
	0x9e, 0xc5, 0xdd, 0x44, 0xe2, 0x45, 0xa0, 0xa9, 0x3b, 0x4c, 0x8f, 0xde, 0x96, 0xa6, 0x7c, 0x22, 
	0x9e, 0xc5, 0xdd, 0x44, 0xe2, 0x45, 0xa0, 0xa9, 0x3b, 0x4c, 0x8f, 0xde, 0x96, 0xa6, 0x7c, 0x22,
	0x9e, 0xc5, 0xdd, 0x44, 0xe2, 0x45, 0xa0, 0xa9, 0x3b, 0x4c, 0x8f, 0xde, 0x96, 0xa6, 0x7c, 0x22, 
	0x9e, 0xc5, 0xdd, 0x44, 0xe2, 0x45, 0xa0, 0xa9, 0x3b, 0x4c, 0x8f, 0xde, 0x96, 0xa6, 0x7c, 0x22,
	0x9e, 0xc5, 0xdd, 0x44, 0xe2, 0x45, 0xa0, 0xa9, 0x3b, 0x4c, 0x8f, 0xde, 0x96, 0xa6, 0x7c, 0x22,
	0x9e, 0xc5, 0xdd, 0x44, 0xe2, 0x45, 0xa0, 0xa9, 0x3b, 0x4c, 0x8f, 0xde, 0x96, 0xa6, 0x7c, 0x22,
	0x9e, 0xc5, 0xdd, 0x44, 0xe2, 0x45, 0xa0, 0xa9, 0x3b, 0x4c, 0x8f, 0xde, 0x96, 0xa6, 0x7c, 0x22, 
	0x9e, 0xc5, 0xdd, 0x44, 0xe2, 0x45, 0xa0, 0xa9, 0x3b, 0x4c, 0x8f, 0xde, 0x96, 0xa6, 0x7c, 0x22,
	0x9e, 0xc5, 0xdd, 0x44, 0xe2, 0x45, 0xa0, 0xa9, 0x3b, 0x4c, 0x8f, 0xde, 0x96, 0xa6, 0x7c, 0x22, 
	0x9e, 0xc5, 0xdd, 0x44, 0xe2,
};

const char snoop_sm3dec_ctrl_data[32] = {
	0x3a, 0xd8, 0xab, 0xb8, 0xc2, 0x28, 0x8d, 0xdc, 0xb2, 0x55, 0x39, 0x72, 0xd5, 0x17, 0xce, 0x27,
	0x2b, 0x7d, 0xe5, 0xa0, 0xea, 0x5e, 0xd8, 0x84, 0xe4, 0x20, 0x14, 0x50, 0x83, 0x17, 0xa2, 0x3b,
};

const char snoop_sm1sm3enc_sm1_ctrl_data[0x105] = {
	0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x1c, 0xa3, 0x18, 0x65, 0x6c, 0x9d, 0x4c, 0xc9, 0x89, 0x21, 0x1a,
	0x68, 0xce, 0x1c, 0x47, 0xd8, 0x0e, 0x10, 0xab, 0xd9, 0x76, 0x95, 0x21, 0x2c, 0x94, 0x48, 0x5a,
	0x4d, 0x75, 0xb6, 0x42, 0x3f, 0x73, 0x1b, 0x3f, 0xb4, 0x10, 0x97, 0xff, 0x35, 0x80, 0x9a, 0x0f,
	0x98, 0x32, 0x5e, 0xda, 0x0e, 0xec, 0x63, 0xc4, 0x25, 0xed, 0xdd, 0x09, 0x2d, 0x7e, 0x26, 0x96,
	0x38, 0x3e, 0xb7, 0x7c, 0x11, 0x14, 0x7a, 0x9c, 0x1a, 0xe9, 0x6b, 0x8e, 0xfd, 0xf7, 0x59, 0x46,
	0x0e, 0x8c, 0x2c, 0x35, 0x7d, 0x84, 0x85, 0x73, 0x8b, 0x62, 0x9b, 0x96, 0x10, 0x3e, 0xf4, 0xfa,
	0x95, 0xd9, 0x96, 0x48, 0xba, 0xbe, 0x45, 0x09, 0x54, 0x66, 0xd0, 0x60, 0xc4, 0x52, 0x92, 0x7e,
	0xda, 0xd2, 0xd9, 0x45, 0x1b, 0x43, 0xb9, 0x5d, 0x03, 0xb7, 0x44, 0xbb, 0xd3, 0xd2, 0x68, 0xd9,
	0xa6, 0x3d, 0x18, 0xe0, 0x21, 0x9f, 0x7f, 0x74, 0x43, 0x2e, 0xfd, 0x9e, 0xb1, 0x0c, 0xeb, 0x3e,
	0x53, 0x83, 0xd3, 0x26, 0xd4, 0xb6, 0x21, 0x11, 0x7c, 0x22, 0xa5, 0x94, 0x75, 0xf7, 0x31, 0x63,
	0x31, 0x81, 0xba, 0x5d, 0xbf, 0x63, 0xfb, 0x9f, 0x2c, 0x18, 0xd2, 0xfa, 0x62, 0x8e, 0x86, 0x4c,
	0xf4, 0xd0, 0x7a, 0xf1, 0x6d, 0x4c, 0x2a, 0x2a, 0x31, 0x69, 0xdf, 0x31, 0x9a, 0x67, 0x2a, 0x28,
	0xf9, 0x30, 0x15, 0xbf, 0x33, 0x2e, 0xcf, 0x0a, 0x3c, 0x04, 0x44, 0x4c, 0xd4, 0xeb, 0xf5, 0xbc,
	0xf7, 0x17, 0x13, 0x58, 0x39, 0x72, 0x08, 0x6d, 0x7b, 0x75, 0xdc, 0x04, 0xd1, 0x0b, 0xf3, 0xb1,
	0x22, 0xba, 0xed, 0xbd, 0x32, 0x9a, 0xc2, 0xde, 0x92, 0x11, 0x54, 0x55, 0x49, 0x5b, 0xc9, 0x1f,
	0xee, 0x0a, 0x60, 0xa2, 0x72, 0xd0, 0xde, 0xa7, 0x0d, 0xcb, 0xba, 0xab, 0x92, 0x7d, 0xb5, 0x4f,
	0x18, 0xc1, 0x93, 0x2b, 0xc5,
};

const char snoop_sm1sm3enc_sm3_ctrl_data[32] = {
	0xb1, 0xf1, 0x35, 0xa2, 0xef, 0xf2, 0x8a, 0x35, 0x64, 0xc7, 0xf7, 0x7e, 0xaf, 0x21, 0x9a, 0x0b,
	0x46, 0x08, 0x09, 0xcb, 0x5f, 0x62, 0xec, 0x66, 0x63, 0x55, 0xf3, 0x2f, 0xb1, 0xb7, 0x13, 0xd8,
};

const char snoop_sm1sm3dec_sm1_ctrl_data[0x105] = {
	0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x1b, 0x19, 0xef, 0x77, 0x38, 0xbd, 0xe4, 0x22, 0xb3, 0xf7, 0x11,
	0xcd, 0x7b, 0x30, 0xe4, 0x94, 0x1b, 0x19, 0xef, 0x77, 0x38, 0xbd, 0xe4, 0x22, 0xb3, 0xf7, 0x11,
	0xcd, 0x7b, 0x30, 0xe4, 0x94, 0x1b, 0x19, 0xef, 0x77, 0x38, 0xbd, 0xe4, 0x22, 0xb3, 0xf7, 0x11,
	0xcd, 0x7b, 0x30, 0xe4, 0x94, 0x1b, 0x19, 0xef, 0x77, 0x38, 0xbd, 0xe4, 0x22, 0xb3, 0xf7, 0x11,
	0xcd, 0x7b, 0x30, 0xe4, 0x94, 0x1b, 0x19, 0xef, 0x77, 0x38, 0xbd, 0xe4, 0x22, 0xb3, 0xf7, 0x11,
	0xcd, 0x7b, 0x30, 0xe4, 0x94, 0x1b, 0x19, 0xef, 0x77, 0x38, 0xbd, 0xe4, 0x22, 0xb3, 0xf7, 0x11,
	0xcd, 0x7b, 0x30, 0xe4, 0x94, 0x1b, 0x19, 0xef, 0x77, 0x38, 0xbd, 0xe4, 0x22, 0xb3, 0xf7, 0x11,
	0xcd, 0x7b, 0x30, 0xe4, 0x94, 0x1b, 0x19, 0xef, 0x77, 0x38, 0xbd, 0xe4, 0x22, 0xb3, 0xf7, 0x11,
	0xcd, 0x7b, 0x30, 0xe4, 0x94, 0x1b, 0x19, 0xef, 0x77, 0x38, 0xbd, 0xe4, 0x22, 0xb3, 0xf7, 0x11,
	0xcd, 0x7b, 0x30, 0xe4, 0x94, 0x1b, 0x19, 0xef, 0x77, 0x38, 0xbd, 0xe4, 0x22, 0xb3, 0xf7, 0x11,
	0xcd, 0x7b, 0x30, 0xe4, 0x94, 0x1b, 0x19, 0xef, 0x77, 0x38, 0xbd, 0xe4, 0x22, 0xb3, 0xf7, 0x11,
	0xcd, 0x7b, 0x30, 0xe4, 0x94, 0x1b, 0x19, 0xef, 0x77, 0x38, 0xbd, 0xe4, 0x22, 0xb3, 0xf7, 0x11,
	0xcd, 0x7b, 0x30, 0xe4, 0x94, 0x1b, 0x19, 0xef, 0x77, 0x38, 0xbd, 0xe4, 0x22, 0xb3, 0xf7, 0x11,
	0xcd, 0x7b, 0x30, 0xe4, 0x94, 0x1b, 0x19, 0xef, 0x77, 0x38, 0xbd, 0xe4, 0x22, 0xb3, 0xf7, 0x11,
	0xcd, 0x7b, 0x30, 0xe4, 0x94, 0x1b, 0x19, 0xef, 0x77, 0x38, 0xbd, 0xe4, 0x22, 0xb3, 0xf7, 0x11,
	0xcd, 0x7b, 0x30, 0xe4, 0x94, 0x1b, 0x19, 0xef, 0x77, 0x38, 0xbd, 0xe4, 0x22, 0xb3, 0xf7, 0x11,
	0xcd, 0x7b, 0x30, 0xe4, 0x94,
};

int inline_cnstr_jobdesc_cipher_sg_pre(struct csec_priv_t *csec_priv,uint32_t *desc,struct cipher_core *cipher,void **sg_virt,dma_addr_t *sg_phy)
{
	struct sg_entry *sg_table;
	int sg_len,fsg_maxlen;
	dma_addr_t sg_addr;
	int rev_len,i=0;
	dma_addr_t sgt_phy_addr;
	
	*sg_virt = (struct sg_entry *)kmalloc(SGMAX*16 + DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	if (!sg_virt) {
		csec_error(KERN_INFO "inline_cnstr_jobdesc_cipher_sg: sg_raw mem error\n");
		return -ENOMEM;
	}
	sg_table = (struct sg_entry *) (((size_t)*sg_virt+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	sg_addr = cipher->data_addr;
	fsg_maxlen = SIZE4KI-16-cipher->key_len-cipher->iv_len;

	fsg_maxlen &= 0xfffffff0;

	rev_len = cipher->data_len;

	while(rev_len)
	{

		sg_len = (rev_len < SIZE4KI)?rev_len: SIZE4KI;
		rev_len -= sg_len;
		sg_table[i].addr_hi = change_addr_for_sec(sg_addr) & 0xffffffff;
		sg_table[i].addr_lo= change_addr_for_sec(sg_addr) >> 32;
		sg_table[i].len_flag = sg_len & SG_ENTRY_LENGTH_MASK;
		sg_table[i].bpid_offset = 0;

		if(rev_len<=0)
		{
			sg_table[i].len_flag |= SG_ENTRY_FINAL_BIT;
		}
		sg_addr += sg_len;
		i++;
	}

	//sec_dump(sg_table,64);

	sgt_phy_addr = dma_map_single(csec_priv->dev,(void *)sg_table,SGMAX*16, DMA_TO_DEVICE);

	*sg_phy = sgt_phy_addr;
	
	return 0;
}



void newapi_desc_done(struct csec_priv_t *csec_priv,void *dma_virt,dma_addr_t desc,uint32_t status, void *arg)
{	
	struct custom_result *op;
	//csec_debug(KERN_INFO "b\n");
	op = arg;
	op->rst = status;
	if(op->sg_virt)
	{
		dma_unmap_single(csec_priv->dev,op->sg_phy,SGMAX*16,DMA_TO_DEVICE);
		kfree(op->sg_virt);
	}

	complete(&op->op_done);
}

int  new_interface_do(struct csec_priv_t *csec_priv,struct cipher_core *cipher,unsigned char cmd_nr,struct custom_result *done_op)
{
	dma_addr_t desc_phy_addr;
	dma_addr_t sg_phy_addr;
	uint32_t *desc;
	int status;

	void *sg_virt;
	
	desc = kmalloc(MAX_CSEC_DESCSIZE,GFP_KERNEL|SYS_DMA);
	if(!desc )
	{
		csec_error(KERN_ERR "cdev_csec_do: desc kzalloc error\n");
		return -ENOMEM;
	}

	if(cmd_nr == ACLASS_CIPHER)
	{		
		if(cipher->data_len + 128 <SIZE4KI)
		{
			 inline_cnstr_jobdesc_cipher_core(desc,cipher);
		}
		else
		{
			inline_cnstr_jobdesc_cipher_sg_pre(csec_priv,desc,cipher,&sg_virt,&sg_phy_addr);
			cipher->data_addr = sg_phy_addr;	
			inline_cnstr_jobdesc_cipher_sg_core(csec_priv,desc,cipher);
			done_op->sg_virt =  sg_virt;
			done_op->sg_phy = sg_phy_addr;
		}
	}
	else if(cmd_nr == ACLASS_HASH)
	{
		inline_cnstr_jobdesc_hash_core(desc,cipher);
	}
	else if(cmd_nr == ACLASS_SNOOP)
	{

		 inline_cnstr_jobdesc_snoop_core(desc,cipher);
	}
	else if(cmd_nr == ACLASS_SNOOP_SP)
	{

		 inline_cnstr_jobdesc_snoop_sp(desc,cipher);
	}
	desc_phy_addr = dma_map_single(csec_priv->dev,(void *)desc,MAX_CSEC_DESCSIZE, DMA_TO_DEVICE);	

	csec_debug(KERN_INFO "desc fill ok\n");

	init_completion(&done_op->op_done);
	
	do
	{
		status = cards_enqueue(csec_priv,  desc, desc_phy_addr,(void *)newapi_desc_done,(void *)done_op);
		if(status)
		{
			if(CDEV_INVL)
			{
				//wait_event_timeout(csec_priv->ccore_cards->dq_done,0,CDEV_INVL);
				set_current_state(TASK_INTERRUPTIBLE);
				schedule_timeout(CDEV_INVL);
				
				csec_debug2(KERN_INFO "ce0\n");
			}
		}
	}while(status==-EBUSY);
	
	csec_debug(KERN_INFO "cdev_csec_ioctl is called s0\n");

	if (!wait_for_completion_timeout(&done_op->op_done, CDEV_INVL*20)){
		csec_error("wait_for_completion_timeout\n");
		return -EAGAIN;
	}
	
	csec_debug(KERN_INFO "cdev_csec_ioctl is called s1, over!\n!");

	dma_unmap_single(csec_priv->dev,desc_phy_addr,MAX_CSEC_DESCSIZE,DMA_TO_DEVICE);

	kfree(desc);
//	dma_free_coherent(csec_priv->dev, MAX_CSEC_DESCSIZE*4, desc, desc_phy_addr);

	return 0;
}

int sm1_cbc_encrypt_newapi(u8 *in,u8 *out,int inlen,u8 *key,u8 *iv)
{
	struct csec_priv_t *csec_priv;
	struct ccore_cards_t *ccore_cards = get_ccore_cards();
	struct cipher_core  cipher;
	struct custom_result *done_op;
	u8 *key_in,*key_in_raw,*iv_in,*iv_in_raw,*data_in,*data_in_raw;
	int ret;
	dma_addr_t data_ori;
	
	cipher.key_len = 32;
	cipher.data_len = inlen;
	cipher.iv_len = 16;
	cipher.alg = SM1;
	cipher.type = CBC;
	cipher.as = INITFINAL;
	cipher.opt = ENC;

	key_in_raw =(u8 *) kmalloc(cipher.key_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	key_in =(u8 *) (((size_t)key_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	iv_in_raw =(u8 *) kmalloc(cipher.iv_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	iv_in = (u8 *) (((size_t)iv_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	data_in_raw =(u8 *) kmalloc(cipher.data_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	data_in = (u8 *) (((size_t)data_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));
	
	done_op = kzalloc(sizeof(struct custom_result),GFP_KERNEL);
	
	csec_priv = cards_enqueue_pre(ccore_cards);

	memcpy(key_in,key,cipher.key_len);
	memcpy(iv_in,iv,cipher.iv_len);
	memcpy(data_in,in,cipher.data_len);

	cipher.key_addr = dma_map_single(csec_priv->dev,(void *)key_in,cipher.key_len, DMA_BIDIRECTIONAL);
	cipher.iv_addr = dma_map_single(csec_priv->dev,(void *)iv_in,cipher.iv_len, DMA_BIDIRECTIONAL);
	cipher.data_addr = dma_map_single(csec_priv->dev,(void *)data_in,cipher.data_len, DMA_BIDIRECTIONAL);
	data_ori = cipher.data_addr;

	new_interface_do(csec_priv,&cipher,ACLASS_CIPHER,done_op);

	do{
		dma_sync_single_for_cpu(csec_priv->dev,data_ori,cipher.data_len,DMA_BIDIRECTIONAL);
		dma_sync_single_for_cpu(csec_priv->dev,cipher.iv_addr,cipher.iv_len,DMA_BIDIRECTIONAL);
		ret = memcmp(data_in,in,cipher.iv_len);
	}while(!ret);


	memcpy(out,data_in,cipher.data_len);
	memcpy(iv,iv_in,cipher.iv_len);
	

	dma_unmap_single(csec_priv->dev,cipher.key_addr,cipher.key_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher.iv_addr,cipher.iv_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,data_ori,cipher.data_len,DMA_BIDIRECTIONAL);

	kfree(key_in_raw);
	kfree(iv_in_raw);
	kfree(data_in_raw);
	
	if(done_op->rst!=0)
	{
		kfree(done_op);
		return -1;
	}else{
		kfree(done_op);
		return 0;
	}
}

int sm1_cbc_decrypt_newapi(u8 *in,u8 *out,int inlen,u8 *key,u8 *iv)
{
	struct csec_priv_t *csec_priv;
	struct ccore_cards_t *ccore_cards = get_ccore_cards();
	struct cipher_core  cipher;
	struct custom_result *done_op;
	u8 *key_in,*key_in_raw,*iv_in,*iv_in_raw,*data_in,*data_in_raw;
	int ret;
	dma_addr_t data_ori;
	
	cipher.key_len = 32;
	cipher.data_len = inlen;
	cipher.iv_len = 16;
	cipher.alg = SM1;
	cipher.type = CBC;
	cipher.as = INITFINAL;
	cipher.opt = DEC;
	key_in_raw =(u8 *) kmalloc(cipher.key_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	key_in =(u8 *) (((size_t)key_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	iv_in_raw =(u8 *) kmalloc(cipher.iv_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	iv_in = (u8 *) (((size_t)iv_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	data_in_raw =(u8 *) kmalloc(cipher.data_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	data_in = (u8 *) (((size_t)data_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));
	
	done_op = kzalloc(sizeof(struct custom_result),GFP_KERNEL);
	
	csec_priv = cards_enqueue_pre(ccore_cards);

	memcpy(key_in,key,cipher.key_len);
	memcpy(iv_in,iv,cipher.iv_len);
	memcpy(data_in,in,cipher.data_len);

	cipher.key_addr = dma_map_single(csec_priv->dev,(void *)key_in,cipher.key_len, DMA_BIDIRECTIONAL);
	cipher.iv_addr = dma_map_single(csec_priv->dev,(void *)iv_in,cipher.iv_len, DMA_BIDIRECTIONAL);
	cipher.data_addr = dma_map_single(csec_priv->dev,(void *)data_in,cipher.data_len, DMA_BIDIRECTIONAL);
	data_ori = cipher.data_addr;
	
	new_interface_do(csec_priv,&cipher,ACLASS_CIPHER,done_op);

	do{
		dma_sync_single_for_cpu(csec_priv->dev,data_ori,cipher.data_len,DMA_BIDIRECTIONAL);
		dma_sync_single_for_cpu(csec_priv->dev,cipher.iv_addr,cipher.iv_len,DMA_BIDIRECTIONAL);
		ret = memcmp(data_in,in,cipher.iv_len);
	}while(!ret);

	memcpy(out,data_in,cipher.data_len);
	memcpy(iv,iv_in,cipher.iv_len);
	
	dma_unmap_single(csec_priv->dev,cipher.key_addr,cipher.key_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher.iv_addr,cipher.iv_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,data_ori,cipher.data_len,DMA_BIDIRECTIONAL);
	kfree(key_in_raw);
	kfree(iv_in_raw);
	kfree(data_in_raw);
	if(done_op->rst!=0)
	{
		kfree(done_op);
		return -1;
	}else
	{
		kfree(done_op);
		return 0;
	}

}

int memcmp_with0(unsigned char *input,int len)
{
	int i;
	for(i=0;i<len;i++)
	{
		if(input[i]!=0)
			return 1;
	}
	return 0;
}

int sm3_hmac_newapi(u8 *key,int keylen,u8 *input,int inlen,u8 *output)
{
	struct csec_priv_t *csec_priv;
	struct ccore_cards_t *ccore_cards = get_ccore_cards();
	struct cipher_core  cipher;
	struct custom_result *done_op;
	u8 *key_in,*key_in_raw,*iv_in,*iv_in_raw,*data_in,*data_in_raw;

	int ret;
	
	cipher.key_len = keylen;
	cipher.data_len = inlen;
	cipher.iv_len = 32;
	cipher.alg = SM3;
	cipher.type = T_HMAC;
	cipher.as = INITFINAL;
	cipher.opt = ENC;
	
	key_in_raw =(u8 *) kmalloc(cipher.key_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	key_in =(u8 *) (((size_t)key_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	iv_in_raw =(u8 *) kmalloc(cipher.iv_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	iv_in = (u8 *) (((size_t)iv_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	data_in_raw =(u8 *) kmalloc(cipher.data_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	data_in = (u8 *) (((size_t)data_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));
	
	done_op = kzalloc(sizeof(struct custom_result),GFP_KERNEL);
	
	csec_priv = cards_enqueue_pre(ccore_cards);

	memcpy(key_in,key,cipher.key_len);
	memcpy(data_in,input,cipher.data_len);
	memset(iv_in,0,cipher.iv_len);

	// 将数据映射到DMA地址
	cipher.key_addr = dma_map_single(csec_priv->dev,(void *)key_in,cipher.key_len, DMA_BIDIRECTIONAL);
	cipher.iv_addr = dma_map_single(csec_priv->dev,(void *)iv_in,cipher.iv_len, DMA_BIDIRECTIONAL);
	cipher.data_addr = dma_map_single(csec_priv->dev,(void *)data_in,cipher.data_len, DMA_BIDIRECTIONAL);
	
	new_interface_do(csec_priv,&cipher,ACLASS_HASH,done_op);

	do{
		dma_sync_single_for_cpu(csec_priv->dev,cipher.iv_addr,cipher.iv_len,DMA_BIDIRECTIONAL);
		ret = memcmp_with0(iv_in,cipher.iv_len);
	}while(!ret);

	memcpy(output,iv_in,cipher.iv_len);
	
	dma_unmap_single(csec_priv->dev,cipher.key_addr,cipher.key_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher.iv_addr,cipher.iv_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher.data_addr,cipher.data_len,DMA_BIDIRECTIONAL);
	kfree(key_in_raw);
	kfree(iv_in_raw);
	kfree(data_in_raw);
	if(done_op->rst!=0)
	{
		kfree(done_op);
		return -1;
	}else
	{
		kfree(done_op);
		return 0;
	}

}

int snoop_sm4cbc_sm3hmac_newapi(u8 *key_class1,u8 *key_class2,int key1len,int key2len,u8 *input,int inlen,u8 *output)
{
	struct csec_priv_t *csec_priv;
	struct ccore_cards_t *ccore_cards = get_ccore_cards();
	struct cipher_core  cipher[2],*cipher_class1,*cipher_class2;
	struct custom_result *done_op;
	u8 *key1_in,*key1_in_raw,*key2_in,*key2_in_raw,*iv_in,*iv_in_raw,*data_in,*data_in_raw,*icv,*icv_raw;

	int ret;

	cipher_class1 = cipher;
	cipher_class2 = &(cipher[1]);
	
	cipher_class1->key_len = key1len;
	cipher_class1->data_len = inlen;
	cipher_class1->iv_len = 16;
	cipher_class1->alg = SM4;
	cipher_class1->type = CBC;
	cipher_class1->as = INITFINAL;
	cipher_class1->opt = ENC;

	cipher_class2->key_len = key2len;
	cipher_class2->data_len = inlen;
	cipher_class2->iv_len = 0;
	cipher_class2->alg = H_SM3;
	cipher_class2->type = T_HMAC;
	cipher_class2->as = INITFINAL;
	cipher_class2->opt = ENC;
	
	key1_in_raw =(u8 *) kmalloc(cipher_class1->key_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	key1_in =(u8 *) (((size_t)key1_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	key2_in_raw =(u8 *) kmalloc(cipher_class2->key_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	key2_in =(u8 *) (((size_t)key2_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	iv_in_raw =(u8 *) kmalloc(cipher_class1->iv_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	iv_in = (u8 *) (((size_t)iv_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	data_in_raw =(u8 *) kmalloc(cipher_class1->data_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	data_in = (u8 *) (((size_t)data_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	icv_raw =(u8 *) kmalloc(80+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	icv = (u8 *) (((size_t)icv_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));
	
	done_op = kzalloc(sizeof(struct custom_result),GFP_KERNEL);

	csec_priv = cards_enqueue_pre(ccore_cards);

	memcpy(key1_in,key_class1,cipher_class1->key_len);
	memcpy(key2_in,key_class2,cipher_class2->key_len);
	memcpy(data_in,input,cipher_class1->data_len);
	memset(iv_in,0,cipher_class1->iv_len);
	memset(icv,0,80);

	cipher_class1->key_addr = dma_map_single(csec_priv->dev,(void *)key1_in,cipher_class1->key_len, DMA_BIDIRECTIONAL);
	cipher_class2->key_addr = dma_map_single(csec_priv->dev,(void *)key2_in,cipher_class2->key_len, DMA_BIDIRECTIONAL);
	cipher_class1->iv_addr = dma_map_single(csec_priv->dev,(void *)iv_in,cipher_class1->iv_len, DMA_BIDIRECTIONAL);
	cipher_class1->data_addr = dma_map_single(csec_priv->dev,(void *)data_in,cipher_class1->data_len, DMA_BIDIRECTIONAL);
	cipher_class2->data_addr = dma_map_single(csec_priv->dev,(void *)icv,80, DMA_BIDIRECTIONAL);
	
	new_interface_do(csec_priv,cipher,ACLASS_SNOOP,done_op);

	do{
		dma_sync_single_for_cpu(csec_priv->dev,cipher_class2->data_addr,80,DMA_BIDIRECTIONAL);
		ret = memcmp_with0(icv,4);
	}while(!ret);

	memcpy(output,icv,4);
	
	dma_unmap_single(csec_priv->dev,cipher_class1->key_addr,cipher_class1->key_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class2->key_addr,cipher_class1->key_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class1->iv_addr,cipher_class1->iv_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class1->data_addr,cipher_class1->data_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class2->data_addr,80,DMA_BIDIRECTIONAL);
	kfree(key1_in_raw);
	kfree(key2_in_raw);
	kfree(iv_in_raw);
	kfree(data_in_raw);
	kfree(icv_raw);
	if(done_op->rst!=0)
	{
		kfree(done_op);
		return -1;
	}else
	{
		kfree(done_op);
		return 0;
	}

}

int sym_newapi_speed_do(void *_mparm)
{
	struct parm *mparm = _mparm;
	struct csec_priv_t *csec_priv;
	struct ccore_cards_t *ccore_cards = get_ccore_cards();
	struct cipher_core  cipher;
	struct custom_result *done_op;
	u8 *key_in,*key_in_raw,*iv_in,*iv_in_raw,*data_in,*data_in_raw;
	u8 data_tmp[4];
	int ret;
	dma_addr_t data_ori;
	int i;
	
	cipher.key_len = mkeysize;
	cipher.data_len = mdatasize;
	cipher.iv_len = mivsize;
	cipher.alg = malg;
	cipher.type = mtype;
	cipher.as = INITFINAL;
	cipher.opt = mopt;
	key_in_raw =(u8 *) kmalloc(cipher.key_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	key_in =(u8 *) (((size_t)key_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	iv_in_raw =(u8 *) kmalloc(cipher.iv_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	iv_in = (u8 *) (((size_t)iv_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	data_in_raw =(u8 *) kmalloc(cipher.data_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	data_in = (u8 *) (((size_t)data_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));
	
	done_op = kzalloc(sizeof(struct custom_result),GFP_KERNEL);
	
	csec_priv = cards_enqueue_pre(ccore_cards);

	get_random_bytes(key_in,mkeysize);
	get_random_bytes(iv_in,mivsize);
	get_random_bytes(data_in,mdatasize);
	cipher.key_addr = dma_map_single(csec_priv->dev,(void *)key_in,cipher.key_len, DMA_BIDIRECTIONAL);
	cipher.iv_addr = dma_map_single(csec_priv->dev,(void *)iv_in,cipher.iv_len, DMA_BIDIRECTIONAL);
	cipher.data_addr = dma_map_single(csec_priv->dev,(void *)data_in,cipher.data_len, DMA_BIDIRECTIONAL);
	data_ori = cipher.data_addr;

	memcpy(data_tmp,data_in+mdatasize-4,4);
	//printk(KERN_ERR "1 mparm is %llx\n",mparm);
	wait_for_completion_interruptible(&(mparm->w));
	
	//printk(KERN_ERR "2 mparm is %llx\n",mparm);
	for(i=0;i<DO_NUM;i++){

		new_interface_do(csec_priv,&cipher,ACLASS_CIPHER,done_op);

		do{
			dma_sync_single_for_cpu(csec_priv->dev,data_ori,cipher.data_len,DMA_BIDIRECTIONAL);
			dma_sync_single_for_cpu(csec_priv->dev,cipher.iv_addr,cipher.iv_len,DMA_BIDIRECTIONAL);
			ret = memcmp(data_tmp,data_in+mdatasize-4,4);
		}while(!ret);
		memcpy(data_tmp,data_in+mdatasize-4,4);
		memset(done_op,0,sizeof(struct custom_result));
		cipher.data_addr = data_ori;
	}
	//printk(KERN_ERR "3 mparm is %llx\n",mparm);
	complete(&(mparm->c));

	//printk(KERN_ERR "4 mparm is %llx\n",mparm);
	
	dma_unmap_single(csec_priv->dev,cipher.key_addr,cipher.key_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher.iv_addr,cipher.iv_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,data_ori,cipher.data_len,DMA_BIDIRECTIONAL);
	kfree(key_in_raw);
	kfree(iv_in_raw);
	kfree(data_in_raw);
	if(done_op->rst!=0)
	{
		kfree(done_op);
		return -1;
	}else
	{
		kfree(done_op);
		return 0;
	}

}


int hash_newapi_speed_do(void *_mparm)
{
	struct parm *mparm = _mparm;
	struct csec_priv_t *csec_priv;
	struct ccore_cards_t *ccore_cards = get_ccore_cards();
	struct cipher_core  cipher;
	struct custom_result *done_op;
	u8 *key_in,*key_in_raw,*iv_in,*iv_in_raw,*data_in,*data_in_raw;
	u8 data_tmp[128];
	int ret;

	int i;
	
	cipher.key_len = mkeysize;
	cipher.data_len = mdatasize;
	cipher.iv_len = mivsize;
	cipher.alg = malg;
	cipher.type = mtype;
	cipher.as = INITFINAL;
	cipher.opt = mopt;
	key_in_raw =(u8 *) kmalloc(cipher.key_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	key_in =(u8 *) (((size_t)key_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	iv_in_raw =(u8 *) kmalloc(cipher.iv_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	iv_in = (u8 *) (((size_t)iv_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	data_in_raw =(u8 *) kmalloc(cipher.data_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	data_in = (u8 *) (((size_t)data_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));
	
	done_op = kzalloc(sizeof(struct custom_result),GFP_KERNEL);
	
	csec_priv = cards_enqueue_pre(ccore_cards);

	get_random_bytes(key_in,mkeysize);
	get_random_bytes(iv_in,mivsize);
	get_random_bytes(data_in,mdatasize);

	cipher.key_addr = dma_map_single(csec_priv->dev,(void *)key_in,cipher.key_len, DMA_BIDIRECTIONAL);
	cipher.iv_addr = dma_map_single(csec_priv->dev,(void *)iv_in,cipher.iv_len, DMA_BIDIRECTIONAL);
	cipher.data_addr = dma_map_single(csec_priv->dev,(void *)data_in,cipher.data_len, DMA_BIDIRECTIONAL);

	memcpy(data_tmp,iv_in+mivsize-4,4);
	//printk(KERN_ERR "1 mparm is %llx\n",mparm);
	wait_for_completion_interruptible(&(mparm->w));
	
	//printk(KERN_ERR "2 mparm is %llx\n",mparm);
	for(i=0;i<DO_NUM;i++){

		new_interface_do(csec_priv,&cipher,ACLASS_HASH,done_op);

		do{
			dma_sync_single_for_cpu(csec_priv->dev,cipher.iv_addr,cipher.iv_len,DMA_BIDIRECTIONAL);
			ret = memcmp(data_tmp,iv_in+mivsize-4,4);
		}while(!ret);
		memset(done_op,0,sizeof(struct custom_result));
	
	}
	//printk(KERN_ERR "3 mparm is %llx\n",mparm);
	complete(&(mparm->c));

	//printk(KERN_ERR "4 mparm is %llx\n",mparm);
	
	dma_unmap_single(csec_priv->dev,cipher.key_addr,cipher.key_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher.iv_addr,cipher.iv_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher.data_addr,cipher.data_len,DMA_BIDIRECTIONAL);
	kfree(key_in_raw);
	kfree(iv_in_raw);
	kfree(data_in_raw);
	if(done_op->rst!=0)
	{
		kfree(done_op);
		return -1;
	}else
	{
		kfree(done_op);
		return 0;
	}

}


int snoop_newapi_speed_do(void *_mparm)
{
	struct parm *mparm = _mparm;
	struct csec_priv_t *csec_priv;
	struct ccore_cards_t *ccore_cards = get_ccore_cards();
	struct cipher_core  cipher[2],*cipher_class1,*cipher_class2;
	struct custom_result *done_op;
	u8 *key1_in,*key1_in_raw,*key2_in,*key2_in_raw,*iv_in,*iv_in_raw,*data_in,*data_in_raw,*icv,*icv_raw;
	u8 data_tmp[4];

	int i,ret;

	cipher_class1 = cipher;
	cipher_class2 = &(cipher[1]);
	
	cipher_class1->key_len = mkeysize;
	cipher_class1->data_len = mdatasize;
	cipher_class1->iv_len = mivsize;
	cipher_class1->alg = malg;
	cipher_class1->type = mtype;
	cipher_class1->as = INITFINAL;
	cipher_class1->opt = mopt;

	cipher_class2->key_len = mkeysize2;
	cipher_class2->alg = malg2;
	cipher_class2->type = mtype2;
	cipher_class2->as = INITFINAL;
	cipher_class2->opt = mopt2;
	
	key1_in_raw =(u8 *) kmalloc(cipher_class1->key_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	key1_in =(u8 *) (((size_t)key1_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	key2_in_raw =(u8 *) kmalloc(cipher_class2->key_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	key2_in =(u8 *) (((size_t)key2_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	iv_in_raw =(u8 *) kmalloc(cipher_class1->iv_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	iv_in = (u8 *) (((size_t)iv_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	data_in_raw =(u8 *) kmalloc(cipher_class1->data_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	data_in = (u8 *) (((size_t)data_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	icv_raw =(u8 *) kmalloc(80+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	icv = (u8 *) (((size_t)icv_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));
	
	done_op = kzalloc(sizeof(struct custom_result),GFP_KERNEL);
	
	csec_priv = cards_enqueue_pre(ccore_cards);

	memset(iv_in,0,cipher_class1->iv_len);
	memset(icv,0,80);
	memcpy(data_tmp,data_in+mdatasize-4,4);

	cipher_class1->key_addr = dma_map_single(csec_priv->dev,(void *)key1_in,cipher_class1->key_len, DMA_BIDIRECTIONAL);
	cipher_class2->key_addr = dma_map_single(csec_priv->dev,(void *)key2_in,cipher_class2->key_len, DMA_BIDIRECTIONAL);
	cipher_class1->iv_addr = dma_map_single(csec_priv->dev,(void *)iv_in,cipher_class1->iv_len, DMA_BIDIRECTIONAL);
	cipher_class1->data_addr = dma_map_single(csec_priv->dev,(void *)data_in,cipher_class1->data_len, DMA_BIDIRECTIONAL);
	cipher_class2->data_addr = dma_map_single(csec_priv->dev,(void *)icv,80, DMA_BIDIRECTIONAL);


	wait_for_completion(&(mparm->w));
	
	//printk(KERN_ERR "2 mparm is %llx\n",mparm);
	for(i=0;i<DO_NUM;i++){

		new_interface_do(csec_priv,cipher,ACLASS_SNOOP,done_op);

		do{
			dma_sync_single_for_cpu(csec_priv->dev,cipher_class2->data_addr,80,DMA_BIDIRECTIONAL);
			ret = memcmp_with0(icv,4);
			dma_sync_single_for_cpu(csec_priv->dev,cipher_class1->data_addr,cipher_class1->data_len,DMA_BIDIRECTIONAL);
			ret = (ret && memcmp(data_tmp,data_in+mdatasize-4,4) );
		}while(!ret);
		memset(done_op,0,sizeof(struct custom_result));
		memset(icv,0,4);
		memcpy(data_tmp,data_in+mdatasize-4,4);
		
	}
	//printk(KERN_ERR "3 mparm is %llx\n",mparm);
	complete(&(mparm->c));
	
	dma_unmap_single(csec_priv->dev,cipher_class1->key_addr,cipher_class1->key_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class2->key_addr,cipher_class1->key_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class1->iv_addr,cipher_class1->iv_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class1->data_addr,cipher_class1->data_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class2->data_addr,80,DMA_BIDIRECTIONAL);
	kfree(key1_in_raw);
	kfree(key2_in_raw);
	kfree(iv_in_raw);
	kfree(data_in_raw);
	kfree(icv_raw);
	
	if(done_op->rst!=0)
	{
		kfree(done_op);
		return -1;
	}else
	{
		kfree(done_op);
		return 0;
	}

}

int snoop_sp_sm4sm3_enc(void)
{
	struct csec_priv_t *csec_priv;
	struct ccore_cards_t *ccore_cards = get_ccore_cards();
	struct cipher_core  cipher[2],*cipher_class1,*cipher_class2;
	struct custom_result *done_op;
	u8 *key1_in,*key1_in_raw,*key2_in,*key2_in_raw,*iv1_in,*iv1_in_raw,*iv2_in,*iv2_in_raw,*data_in,*data_in_raw,*data_c,*data_c_raw;
	u8 data_tmp[4];

	int ret;

	int cdatel = 0x100;
	int hdatel = 0x105;

	cipher_class1 = cipher;
	cipher_class2 = &(cipher[1]);
	
	cipher_class1->key_len = 16;
	cipher_class1->data_len = cdatel;
	cipher_class1->iv_len = 16;
	cipher_class1->alg = SM4;
	cipher_class1->type = CBC;
	cipher_class1->as = INITFINAL;
	cipher_class1->opt = ENC;

	cipher_class2->key_len = 32;
	cipher_class2->data_len = hdatel;
	cipher_class2->iv_len = 32;
	cipher_class2->alg = H_SM3;
	cipher_class2->type = T_HMAC;
	cipher_class2->as = INITFINAL;
	cipher_class2->opt = ENC;
	
	key1_in_raw =(u8 *) kmalloc(cipher_class1->key_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	key1_in =(u8 *) (((size_t)key1_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	key2_in_raw =(u8 *) kmalloc(cipher_class2->key_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	key2_in =(u8 *) (((size_t)key2_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	iv1_in_raw =(u8 *) kmalloc(cipher_class1->iv_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	iv1_in = (u8 *) (((size_t)iv1_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	iv2_in_raw =(u8 *) kmalloc(cipher_class2->iv_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	iv2_in = (u8 *) (((size_t)iv2_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	data_in_raw =(u8 *) kmalloc(cipher_class2->data_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	data_in = (u8 *) (((size_t)data_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	data_c_raw =(u8 *) kmalloc(cipher_class2->data_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	data_c = (u8 *) (((size_t)data_c_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));
	
	done_op = kzalloc(sizeof(struct custom_result),GFP_KERNEL);
	
	csec_priv = cards_enqueue_pre(ccore_cards);

	memset(iv2_in,0,cipher_class2->iv_len);

	memset(key1_in,0x5a,cipher_class1->key_len);
	memset(key2_in,0x5a,cipher_class2->key_len);
	memset(iv1_in,0x5a,cipher_class1->iv_len);
	memset(data_in,0x5a,cipher_class2->data_len);
	memcpy(data_tmp,data_in+hdatel-4,4);

	cipher_class1->key_addr = dma_map_single(csec_priv->dev,(void *)key1_in,cipher_class1->key_len, DMA_BIDIRECTIONAL);
	cipher_class2->key_addr = dma_map_single(csec_priv->dev,(void *)key2_in,cipher_class2->key_len, DMA_BIDIRECTIONAL);
	cipher_class1->iv_addr = dma_map_single(csec_priv->dev,(void *)iv1_in,cipher_class1->iv_len, DMA_BIDIRECTIONAL);
	cipher_class2->iv_addr = dma_map_single(csec_priv->dev,(void *)iv2_in,cipher_class2->iv_len, DMA_BIDIRECTIONAL);
	cipher_class2->data_addr = dma_map_single(csec_priv->dev,(void *)data_in,cipher_class2->data_len, DMA_BIDIRECTIONAL);
	cipher_class1->data_addr = cipher_class2->data_addr+hdatel-cdatel;

	new_interface_do(csec_priv,cipher,ACLASS_SNOOP_SP,done_op);

	do{
		dma_sync_single_for_cpu(csec_priv->dev,cipher_class2->iv_addr,cipher_class2->iv_len,DMA_BIDIRECTIONAL);
		ret = memcmp_with0(iv2_in,4);
		dma_sync_single_for_cpu(csec_priv->dev,cipher_class1->data_addr,cipher_class1->data_len,DMA_BIDIRECTIONAL);
		ret = (ret && memcmp(data_tmp,data_in+hdatel-4,4));
	}while(!ret);
	memset(done_op,0,sizeof(struct custom_result));
#if 1
	printk(KERN_ERR "after snoop SM4&SM3 enc,data\n");
	sec_dump(data_in,cipher_class2->data_len);
	printk(KERN_ERR "after snoop SM4&SM3 enc,iv\n");
	sec_dump(iv2_in,cipher_class2->iv_len);
#endif
	memcpy(data_c,data_in,cipher_class2->data_len);

	cipher_class1 = cipher;	
	cipher_class1->key_len = 16;
	cipher_class1->data_len = cdatel;
	cipher_class1->iv_len = 16;
	cipher_class1->alg = SM4;
	cipher_class1->type = CBC;
	cipher_class1->as = INITFINAL;
	cipher_class1->opt = ENC;
	memset(key1_in,0x5a,cipher_class1->key_len);
	memset(iv1_in,0x5a,cipher_class1->iv_len);
	memset(data_in,0x5a,hdatel);

	memcpy(data_tmp,data_in+hdatel-4,4);

	dma_sync_single_for_device(csec_priv->dev,cipher_class1->key_addr,cipher_class1->key_len,DMA_BIDIRECTIONAL);
	dma_sync_single_for_device(csec_priv->dev,cipher_class1->iv_addr,cipher_class1->iv_len,DMA_BIDIRECTIONAL);
	dma_sync_single_for_device(csec_priv->dev,cipher_class1->data_addr,hdatel,DMA_BIDIRECTIONAL);

	new_interface_do(csec_priv,cipher,ACLASS_CIPHER,done_op);
	do{
		dma_sync_single_for_cpu(csec_priv->dev,cipher_class1->data_addr,cipher_class1->data_len,DMA_BIDIRECTIONAL);
		ret = memcmp(data_tmp,data_in+cdatel-4,4);
	}while(!ret);
	memset(done_op,0,sizeof(struct custom_result));
#if 1
	printk(KERN_ERR "after sm4 enc,data\n");
	sec_dump(data_in,hdatel);
#endif
	cipher_class2 = &(cipher[1]);

	cipher_class2->key_len = 32;
	cipher_class2->data_len = hdatel;
	cipher_class2->iv_len = 32;
	cipher_class2->alg = H_SM3;
	cipher_class2->type = T_HMAC;
	cipher_class2->as = INITFINAL;
	cipher_class2->opt = ENC;

	memset(key2_in,0x5a,cipher_class2->key_len);
//	memset(data_in,0x5a,hdatel);
	memset(iv2_in,0,cipher_class2->iv_len);
	dma_sync_single_for_device(csec_priv->dev,cipher_class2->key_addr,cipher_class2->key_len,DMA_BIDIRECTIONAL);
	dma_sync_single_for_device(csec_priv->dev,cipher_class2->iv_addr,cipher_class2->iv_len,DMA_BIDIRECTIONAL);
	dma_sync_single_for_device(csec_priv->dev,cipher_class2->data_addr,hdatel,DMA_BIDIRECTIONAL);
	new_interface_do(csec_priv,cipher_class2,ACLASS_HASH,done_op);
	do{
		dma_sync_single_for_cpu(csec_priv->dev,cipher_class2->iv_addr,cipher_class2->iv_len,DMA_BIDIRECTIONAL);
		ret = memcmp_with0(iv2_in,4);
	}while(!ret);
	memset(done_op,0,sizeof(struct custom_result));
#if 1
	printk(KERN_ERR "after sm3 hmac,data\n");
	sec_dump(iv2_in,cipher_class2->iv_len);
#endif
	dma_unmap_single(csec_priv->dev,cipher_class1->key_addr,cipher_class1->key_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class2->key_addr,cipher_class1->key_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class1->iv_addr,cipher_class1->iv_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class2->iv_addr,cipher_class2->iv_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class2->data_addr,cipher_class2->data_len,DMA_BIDIRECTIONAL);

	kfree(key1_in_raw);
	kfree(key2_in_raw);
	kfree(iv1_in_raw);
	kfree(iv2_in_raw);
	kfree(data_in_raw);
	kfree(data_c_raw);
	
	if(done_op->rst!=0)
	{
		kfree(done_op);
		return -1;
	}else
	{
		kfree(done_op);
		return 0;
	}

}

int snoop_sp_sm4sm3_dec(void)
{
	struct csec_priv_t *csec_priv;
	struct ccore_cards_t *ccore_cards = get_ccore_cards();
	struct cipher_core  cipher[2],*cipher_class1,*cipher_class2;
	struct custom_result *done_op;
	u8 *key1_in,*key1_in_raw,*key2_in,*key2_in_raw,*iv1_in,*iv1_in_raw,*iv2_in,*iv2_in_raw,*data_in,*data_in_raw;
	u8 data_tmp[4];

	int ret;

	int cdatel = 0x100;
	int hdatel = 0x105;

	cipher_class1 = cipher;
	cipher_class2 = &(cipher[1]);
	
	cipher_class1->key_len = 16;
	cipher_class1->data_len = cdatel;
	cipher_class1->iv_len = 16;
	cipher_class1->alg = SM4;
	cipher_class1->type = CBC;
	cipher_class1->as = INITFINAL;
	cipher_class1->opt = DEC;

	cipher_class2->key_len = 32;
	cipher_class2->data_len = hdatel;
	cipher_class2->iv_len = 32;
	cipher_class2->alg = H_SM3;
	cipher_class2->type = T_HMAC;
	cipher_class2->as = INITFINAL;
	cipher_class2->opt = DEC;
	
	key1_in_raw =(u8 *) kmalloc(cipher_class1->key_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	key1_in =(u8 *) (((size_t)key1_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	key2_in_raw =(u8 *) kmalloc(cipher_class2->key_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	key2_in =(u8 *) (((size_t)key2_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	iv1_in_raw =(u8 *) kmalloc(cipher_class1->iv_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	iv1_in = (u8 *) (((size_t)iv1_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	iv2_in_raw =(u8 *) kmalloc(cipher_class2->iv_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	iv2_in = (u8 *) (((size_t)iv2_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	data_in_raw =(u8 *) kmalloc(cipher_class2->data_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	data_in = (u8 *) (((size_t)data_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));
	
	done_op = kzalloc(sizeof(struct custom_result),GFP_KERNEL);
	
	csec_priv = cards_enqueue_pre(ccore_cards);

	memset(iv2_in,0,cipher_class2->iv_len);

	memset(key1_in,0x5a,cipher_class1->key_len);
	memset(key2_in,0x5a,cipher_class2->key_len);
	memset(iv1_in,0x5a,cipher_class1->iv_len);
	memset(data_in,0x5a,cipher_class2->data_len);
	memcpy(data_tmp,data_in+hdatel-4,4);

	cipher_class1->key_addr = dma_map_single(csec_priv->dev,(void *)key1_in,cipher_class1->key_len, DMA_BIDIRECTIONAL);
	cipher_class2->key_addr = dma_map_single(csec_priv->dev,(void *)key2_in,cipher_class2->key_len, DMA_BIDIRECTIONAL);
	cipher_class1->iv_addr = dma_map_single(csec_priv->dev,(void *)iv1_in,cipher_class1->iv_len, DMA_BIDIRECTIONAL);
	cipher_class2->iv_addr = dma_map_single(csec_priv->dev,(void *)iv2_in,cipher_class2->iv_len, DMA_BIDIRECTIONAL);
	cipher_class2->data_addr = dma_map_single(csec_priv->dev,(void *)data_in,cipher_class2->data_len, DMA_BIDIRECTIONAL);
	cipher_class1->data_addr = cipher_class2->data_addr+hdatel-cdatel;

	new_interface_do(csec_priv,cipher,ACLASS_SNOOP_SP,done_op);

	do{
		dma_sync_single_for_cpu(csec_priv->dev,cipher_class2->iv_addr,cipher_class2->iv_len,DMA_BIDIRECTIONAL);
		ret = memcmp_with0(iv2_in,4);
		dma_sync_single_for_cpu(csec_priv->dev,cipher_class1->data_addr,cipher_class1->data_len,DMA_BIDIRECTIONAL);
		ret = (ret && memcmp(data_tmp,data_in+hdatel-4,4));
	}while(!ret);
	memset(done_op,0,sizeof(struct custom_result));

#if 1
	printk(KERN_ERR "after SM4&SM3 dec,data\n");
	sec_dump(data_in,cipher_class2->data_len);
	printk(KERN_ERR "after SM4&SM3 dec,iv\n");
	sec_dump(iv2_in,cipher_class2->iv_len);
#endif
	cipher_class2 = &(cipher[1]);

	cipher_class2->key_len = 32;
	cipher_class2->data_len = hdatel;
	cipher_class2->iv_len = 32;
	cipher_class2->alg = H_SM3;
	cipher_class2->type = T_HMAC;
	cipher_class2->as = INITFINAL;
	cipher_class2->opt = DEC;

	memset(key2_in,0x5a,cipher_class2->key_len);
	memset(data_in,0x5a,hdatel);
	memset(iv2_in,0,cipher_class2->iv_len);
	dma_sync_single_for_device(csec_priv->dev,cipher_class2->key_addr,cipher_class2->key_len,DMA_BIDIRECTIONAL);
	dma_sync_single_for_device(csec_priv->dev,cipher_class2->iv_addr,cipher_class2->iv_len,DMA_BIDIRECTIONAL);
	dma_sync_single_for_device(csec_priv->dev,cipher_class2->data_addr,hdatel,DMA_BIDIRECTIONAL);
	new_interface_do(csec_priv,cipher_class2,ACLASS_HASH,done_op);
	do{
		dma_sync_single_for_cpu(csec_priv->dev,cipher_class2->iv_addr,cipher_class2->iv_len,DMA_BIDIRECTIONAL);
		ret = memcmp_with0(iv2_in,4);
	}while(!ret);
	memset(done_op,0,sizeof(struct custom_result));
#if 1
	printk(KERN_ERR "after sm3 hmac,data\n");
	sec_dump(iv2_in,cipher_class2->iv_len);
#endif
	cipher_class1 = cipher;	
	cipher_class1->key_len = 16;
	cipher_class1->data_len = cdatel;
	cipher_class1->iv_len = 16;
	cipher_class1->alg = SM4;
	cipher_class1->type = CBC;
	cipher_class1->as = INITFINAL;
	cipher_class1->opt = DEC;
	memset(key1_in,0x5a,cipher_class1->key_len);
	memset(iv1_in,0x5a,cipher_class1->iv_len);
	//memset(data_in,0x5a,hdatel);

	memcpy(data_tmp,data_in+hdatel-4,4);

	dma_sync_single_for_device(csec_priv->dev,cipher_class1->key_addr,cipher_class1->key_len,DMA_BIDIRECTIONAL);
	dma_sync_single_for_device(csec_priv->dev,cipher_class1->iv_addr,cipher_class1->iv_len,DMA_BIDIRECTIONAL);
	dma_sync_single_for_device(csec_priv->dev,cipher_class1->data_addr,hdatel,DMA_BIDIRECTIONAL);

	new_interface_do(csec_priv,cipher,ACLASS_CIPHER,done_op);
	do{
		dma_sync_single_for_cpu(csec_priv->dev,cipher_class1->data_addr,cipher_class1->data_len,DMA_BIDIRECTIONAL);
		ret = memcmp(data_tmp,data_in+cdatel-4,4);
	}while(!ret);
	memset(done_op,0,sizeof(struct custom_result));
#if 1
	printk(KERN_ERR "after sm4 enc,data\n");
	sec_dump(data_in,hdatel);
#endif	
	dma_unmap_single(csec_priv->dev,cipher_class1->key_addr,cipher_class1->key_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class2->key_addr,cipher_class1->key_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class1->iv_addr,cipher_class1->iv_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class2->iv_addr,cipher_class2->iv_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class2->data_addr,cipher_class2->data_len,DMA_BIDIRECTIONAL);
	kfree(key1_in_raw);
	kfree(key2_in_raw);
	kfree(iv1_in_raw);
	kfree(iv2_in_raw);
	kfree(data_in_raw);
	
	if(done_op->rst!=0)
	{
		kfree(done_op);
		return -1;
	}else
	{
		kfree(done_op);
		return 0;
	}

}

int snoop_sp_sm1sm3_enc(void)
{
	struct csec_priv_t *csec_priv;
	struct ccore_cards_t *ccore_cards = get_ccore_cards();
	struct cipher_core  cipher[2],*cipher_class1,*cipher_class2;
	struct custom_result *done_op;
	u8 *key1_in,*key1_in_raw,*key2_in,*key2_in_raw,*iv1_in,*iv1_in_raw,*iv2_in,*iv2_in_raw,*data_in,*data_in_raw,*data_c,*data_c_raw;
	u8 data_tmp[4];
	u8 *snoop_data_0;
	u8 *snoop_data_1;

	int ret;

	int cdatel = 0x100;
	int hdatel = 0x105;

	cipher_class1 = cipher;
	cipher_class2 = &(cipher[1]);
	
	cipher_class1->key_len = 32;
	cipher_class1->data_len = cdatel;
	cipher_class1->iv_len = 16;
	cipher_class1->alg = SM1;
	cipher_class1->type = CBC;
	cipher_class1->as = INITFINAL;
	cipher_class1->opt = ENC;

	cipher_class2->key_len = 32;
	cipher_class2->data_len = hdatel;
	cipher_class2->iv_len = 32;
	cipher_class2->alg = H_SM3;
	cipher_class2->type = T_HMAC;
	cipher_class2->as = INITFINAL;
	cipher_class2->opt = ENC;
	
	key1_in_raw =(u8 *) kmalloc(cipher_class1->key_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	key1_in =(u8 *) (((size_t)key1_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	key2_in_raw =(u8 *) kmalloc(cipher_class2->key_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	key2_in =(u8 *) (((size_t)key2_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	iv1_in_raw =(u8 *) kmalloc(cipher_class1->iv_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	iv1_in = (u8 *) (((size_t)iv1_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	iv2_in_raw =(u8 *) kmalloc(cipher_class2->iv_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	iv2_in = (u8 *) (((size_t)iv2_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	data_in_raw =(u8 *) kmalloc(cipher_class2->data_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	data_in = (u8 *) (((size_t)data_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	data_c_raw =(u8 *) kmalloc(cipher_class2->data_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	data_c = (u8 *) (((size_t)data_c_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));
	
	done_op = kzalloc(sizeof(struct custom_result),GFP_KERNEL);
	
	csec_priv = cards_enqueue_pre(ccore_cards);

	memset(iv2_in,0,cipher_class2->iv_len);

	memset(key1_in,0x5a,cipher_class1->key_len);
	memset(key2_in,0x5a,cipher_class2->key_len);
	memset(iv1_in,0x5a,cipher_class1->iv_len);
	memset(data_in,0x5a,cipher_class2->data_len);
	memcpy(data_tmp,data_in+hdatel-4,4);

	cipher_class1->key_addr = dma_map_single(csec_priv->dev,(void *)key1_in,cipher_class1->key_len, DMA_BIDIRECTIONAL);
	cipher_class2->key_addr = dma_map_single(csec_priv->dev,(void *)key2_in,cipher_class2->key_len, DMA_BIDIRECTIONAL);
	cipher_class1->iv_addr = dma_map_single(csec_priv->dev,(void *)iv1_in,cipher_class1->iv_len, DMA_BIDIRECTIONAL);
	cipher_class2->iv_addr = dma_map_single(csec_priv->dev,(void *)iv2_in,cipher_class2->iv_len, DMA_BIDIRECTIONAL);
	cipher_class2->data_addr = dma_map_single(csec_priv->dev,(void *)data_in,cipher_class2->data_len, DMA_BIDIRECTIONAL);
	cipher_class1->data_addr = cipher_class2->data_addr+hdatel-cdatel;

	new_interface_do(csec_priv,cipher,ACLASS_SNOOP_SP,done_op);

	do{
		dma_sync_single_for_cpu(csec_priv->dev,cipher_class2->iv_addr,cipher_class2->iv_len,DMA_BIDIRECTIONAL);
		ret = memcmp_with0(iv2_in,4);
		dma_sync_single_for_cpu(csec_priv->dev,cipher_class1->data_addr,cipher_class1->data_len,DMA_BIDIRECTIONAL);
		ret = (ret && memcmp(data_tmp,data_in+hdatel-4,4));
	}while(!ret);
	memset(done_op,0,sizeof(struct custom_result));

	snoop_data_0 = kmalloc(cipher_class2->data_len, GFP_KERNEL);
	snoop_data_1 = kmalloc(cipher_class2->iv_len, GFP_KERNEL);
	memcpy(snoop_data_0, data_in, cipher_class2->data_len);
	memcpy(snoop_data_1, iv2_in, cipher_class2->iv_len);
#if 0 //For debug
	printk(KERN_ERR "after snoop SM1 enc,data\n");
	sec_dump(data_in,cipher_class2->data_len);
	printk(KERN_ERR "after snoop SM3 enc,iv\n");
	sec_dump(iv2_in,cipher_class2->iv_len);
#endif
	memcpy(data_c,data_in,cipher_class2->data_len);

	cipher_class1 = cipher;	
	cipher_class1->key_len = 32;
	cipher_class1->data_len = cdatel;
	cipher_class1->iv_len = 16;
	cipher_class1->alg = SM1;
	cipher_class1->type = CBC;
	cipher_class1->as = INITFINAL;
	cipher_class1->opt = ENC;
	memset(key1_in,0x5a,cipher_class1->key_len);
	memset(iv1_in,0x5a,cipher_class1->iv_len);
	memset(data_in,0x5a,hdatel);

	memcpy(data_tmp,data_in+hdatel-4,4);

	dma_sync_single_for_device(csec_priv->dev,cipher_class1->key_addr,cipher_class1->key_len,DMA_BIDIRECTIONAL);
	dma_sync_single_for_device(csec_priv->dev,cipher_class1->iv_addr,cipher_class1->iv_len,DMA_BIDIRECTIONAL);
	dma_sync_single_for_device(csec_priv->dev,cipher_class1->data_addr,hdatel,DMA_BIDIRECTIONAL);

	new_interface_do(csec_priv,cipher,ACLASS_CIPHER,done_op);
	do{
		dma_sync_single_for_cpu(csec_priv->dev,cipher_class1->data_addr,cipher_class1->data_len,DMA_BIDIRECTIONAL);
		ret = memcmp(data_tmp,data_in+cdatel-4,4);
	}while(!ret);
	memset(done_op,0,sizeof(struct custom_result));
	
	if(memcmp(snoop_data_0, data_in, cipher_class2->data_len)) {
		printk("SNOOP SM1&SM3 ENC: SM1 ENC function test failed!\n");
	}else {
		printk("SNOOP SM1&SM3 ENC: SM1 ENC function test PASSED!\n");
	}
#if 0 
	printk(KERN_ERR "after sm1 enc,data\n");
	sec_dump(data_in,hdatel);
#endif
	cipher_class2 = &(cipher[1]);
	cipher_class2->key_len = 32;
	cipher_class2->data_len = hdatel;
	cipher_class2->iv_len = 32;
	cipher_class2->alg = H_SM3;
	cipher_class2->type = T_HMAC;
	cipher_class2->as = INITFINAL;
	cipher_class2->opt = ENC;

	memset(key2_in,0x5a,cipher_class2->key_len);
//	memset(data_in,0x5a,hdatel);
	memset(iv2_in,0,cipher_class2->iv_len);
	dma_sync_single_for_device(csec_priv->dev,cipher_class2->key_addr,cipher_class2->key_len,DMA_BIDIRECTIONAL);
	dma_sync_single_for_device(csec_priv->dev,cipher_class2->iv_addr,cipher_class2->iv_len,DMA_BIDIRECTIONAL);
	dma_sync_single_for_device(csec_priv->dev,cipher_class2->data_addr,hdatel,DMA_BIDIRECTIONAL);
	new_interface_do(csec_priv,cipher_class2,ACLASS_HASH,done_op);
	do{
		dma_sync_single_for_cpu(csec_priv->dev,cipher_class2->iv_addr,cipher_class2->iv_len,DMA_BIDIRECTIONAL);
		ret = memcmp_with0(iv2_in,4);
	}while(!ret);
	memset(done_op,0,sizeof(struct custom_result));

	if(memcmp(snoop_data_1, iv2_in, cipher_class2->iv_len)) {
		printk("SNOOP SM1&SM3 ENC: SM3 ENC function test failed!\n");
	}else {
		printk("SNOOP SM1&SM3 ENC: SM3 ENC function test PASSED!\n");
	}
#if 0
	printk(KERN_ERR "after sm3 hmac,data\n");
	sec_dump(iv2_in,cipher_class2->iv_len);
#endif
	dma_unmap_single(csec_priv->dev,cipher_class1->key_addr,cipher_class1->key_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class2->key_addr,cipher_class1->key_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class1->iv_addr,cipher_class1->iv_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class2->iv_addr,cipher_class2->iv_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class2->data_addr,cipher_class2->data_len,DMA_BIDIRECTIONAL);

	kfree(key1_in_raw);
	kfree(key2_in_raw);
	kfree(iv1_in_raw);
	kfree(iv2_in_raw);
	kfree(data_in_raw);
	kfree(data_c_raw);
	
	kfree(snoop_data_0);
	kfree(snoop_data_1);

	if(done_op->rst!=0)
	{
		kfree(done_op);
		return -1;
	}else
	{
		kfree(done_op);
		return 0;
	}

}

int snoop_sp_sm1sm3_dec(void)
{
	struct csec_priv_t *csec_priv;
	struct ccore_cards_t *ccore_cards = get_ccore_cards();
	struct cipher_core  cipher[2],*cipher_class1,*cipher_class2;
	struct custom_result *done_op;
	u8 *key1_in,*key1_in_raw,*key2_in,*key2_in_raw,*iv1_in,*iv1_in_raw,*iv2_in,*iv2_in_raw,*data_in,*data_in_raw;
	u8 data_tmp[4];
	u8 *snoop_data_0;
	u8 *snoop_data_1;

	int ret;

	int cdatel = 0x100;
	int hdatel = 0x105;

	cipher_class1 = cipher;
	cipher_class2 = &(cipher[1]);
	
	cipher_class1->key_len = 32;
	cipher_class1->data_len = cdatel;
	cipher_class1->iv_len = 16;
	cipher_class1->alg = SM1;
	cipher_class1->type = CBC;
	cipher_class1->as = INITFINAL;
	cipher_class1->opt = DEC;

	cipher_class2->key_len = 32;
	cipher_class2->data_len = hdatel;
	cipher_class2->iv_len = 32;
	cipher_class2->alg = H_SM3;
	cipher_class2->type = T_HMAC;
	cipher_class2->as = INITFINAL;
	cipher_class2->opt = DEC;
	
	key1_in_raw =(u8 *) kmalloc(cipher_class1->key_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	key1_in =(u8 *) (((size_t)key1_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	key2_in_raw =(u8 *) kmalloc(cipher_class2->key_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	key2_in =(u8 *) (((size_t)key2_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	iv1_in_raw =(u8 *) kmalloc(cipher_class1->iv_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	iv1_in = (u8 *) (((size_t)iv1_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	iv2_in_raw =(u8 *) kmalloc(cipher_class2->iv_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	iv2_in = (u8 *) (((size_t)iv2_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	data_in_raw =(u8 *) kmalloc(cipher_class2->data_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	data_in = (u8 *) (((size_t)data_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));
	
	done_op = kzalloc(sizeof(struct custom_result),GFP_KERNEL);
	
	csec_priv = cards_enqueue_pre(ccore_cards);

	memset(iv2_in,0,cipher_class2->iv_len);

	memset(key1_in,0x5a,cipher_class1->key_len);
	memset(key2_in,0x5a,cipher_class2->key_len);
	memset(iv1_in,0x5a,cipher_class1->iv_len);
	memset(data_in,0x5a,cipher_class2->data_len);
	memcpy(data_tmp,data_in+hdatel-4,4);

	cipher_class1->key_addr = dma_map_single(csec_priv->dev,(void *)key1_in,cipher_class1->key_len, DMA_BIDIRECTIONAL);
	cipher_class2->key_addr = dma_map_single(csec_priv->dev,(void *)key2_in,cipher_class2->key_len, DMA_BIDIRECTIONAL);
	cipher_class1->iv_addr = dma_map_single(csec_priv->dev,(void *)iv1_in,cipher_class1->iv_len, DMA_BIDIRECTIONAL);
	cipher_class2->iv_addr = dma_map_single(csec_priv->dev,(void *)iv2_in,cipher_class2->iv_len, DMA_BIDIRECTIONAL);
	cipher_class2->data_addr = dma_map_single(csec_priv->dev,(void *)data_in,cipher_class2->data_len, DMA_BIDIRECTIONAL);
	cipher_class1->data_addr = cipher_class2->data_addr+hdatel-cdatel;

	new_interface_do(csec_priv,cipher,ACLASS_SNOOP_SP,done_op);

	do{
		dma_sync_single_for_cpu(csec_priv->dev,cipher_class2->iv_addr,cipher_class2->iv_len,DMA_BIDIRECTIONAL);
		ret = memcmp_with0(iv2_in,4);
		dma_sync_single_for_cpu(csec_priv->dev,cipher_class1->data_addr,cipher_class1->data_len,DMA_BIDIRECTIONAL);
		ret = (ret && memcmp(data_tmp,data_in+hdatel-4,4));
	}while(!ret);
	memset(done_op,0,sizeof(struct custom_result));

	snoop_data_0 = kmalloc(cipher_class2->data_len, GFP_KERNEL);
	snoop_data_1 = kmalloc(cipher_class2->iv_len, GFP_KERNEL);
	memcpy(snoop_data_0, data_in, cipher_class2->data_len);
	memcpy(snoop_data_1, iv2_in, cipher_class2->iv_len);
#if 0
	printk(KERN_ERR "after dec,data\n");
	sec_dump(data_in,cipher_class2->data_len);
	printk(KERN_ERR "after dec,iv\n");
	sec_dump(iv2_in,cipher_class2->iv_len);
#endif
	cipher_class2 = &(cipher[1]);

	cipher_class2->key_len = 32;
	cipher_class2->data_len = hdatel;
	cipher_class2->iv_len = 32;
	cipher_class2->alg = H_SM3;
	cipher_class2->type = T_HMAC;
	cipher_class2->as = INITFINAL;
	cipher_class2->opt = DEC;

	memset(key2_in,0x5a,cipher_class2->key_len);
	memset(data_in,0x5a,hdatel);
	memset(iv2_in,0,cipher_class2->iv_len);
	dma_sync_single_for_device(csec_priv->dev,cipher_class2->key_addr,cipher_class2->key_len,DMA_BIDIRECTIONAL);
	dma_sync_single_for_device(csec_priv->dev,cipher_class2->iv_addr,cipher_class2->iv_len,DMA_BIDIRECTIONAL);
	dma_sync_single_for_device(csec_priv->dev,cipher_class2->data_addr,hdatel,DMA_BIDIRECTIONAL);
	new_interface_do(csec_priv,cipher_class2,ACLASS_HASH,done_op);
	do{
		dma_sync_single_for_cpu(csec_priv->dev,cipher_class2->iv_addr,cipher_class2->iv_len,DMA_BIDIRECTIONAL);
		ret = memcmp_with0(iv2_in,4);
	}while(!ret);
	memset(done_op,0,sizeof(struct custom_result));

	if(memcmp(snoop_data_1, iv2_in, cipher_class2->iv_len)) {
		printk("SNOOP SM1&SM3 DEC: SM3 DEC function test failed!\n");
	}else {
		printk("SNOOP SM1&SM3 DEC: SM3 DEC function test PASSED!\n");
	}
#if 0
	printk(KERN_ERR "after sm3 hmac,data\n");
	sec_dump(iv2_in,cipher_class2->iv_len);
#endif
	cipher_class1 = cipher;	
	cipher_class1->key_len = 32;
	cipher_class1->data_len = cdatel;
	cipher_class1->iv_len = 16;
	cipher_class1->alg = SM1;
	cipher_class1->type = CBC;
	cipher_class1->as = INITFINAL;
	cipher_class1->opt = DEC;
	memset(key1_in,0x5a,cipher_class1->key_len);
	memset(iv1_in,0x5a,cipher_class1->iv_len);
	//memset(data_in,0x5a,hdatel);

	memcpy(data_tmp,data_in+hdatel-4,4);

	dma_sync_single_for_device(csec_priv->dev,cipher_class1->key_addr,cipher_class1->key_len,DMA_BIDIRECTIONAL);
	dma_sync_single_for_device(csec_priv->dev,cipher_class1->iv_addr,cipher_class1->iv_len,DMA_BIDIRECTIONAL);
	dma_sync_single_for_device(csec_priv->dev,cipher_class1->data_addr,hdatel,DMA_BIDIRECTIONAL);

	new_interface_do(csec_priv,cipher,ACLASS_CIPHER,done_op);
	do{
		dma_sync_single_for_cpu(csec_priv->dev,cipher_class1->data_addr,cipher_class1->data_len,DMA_BIDIRECTIONAL);
		ret = memcmp(data_tmp,data_in+cdatel-4,4);
	}while(!ret);
	memset(done_op,0,sizeof(struct custom_result));

	if(memcmp(snoop_data_0, data_in, cipher_class2->data_len)) {
		printk("SNOOP SM1&SM3 DEC: SM3 DEC function test failed!\n");
	}else {
		printk("SNOOP SM1&SM3 DEC: SM3 DEC function test PASSED!\n");
	}
#if 0 //For debug
	printk(KERN_ERR "after sm1 dec,data\n");
	sec_dump(data_in,hdatel);
#endif	
	dma_unmap_single(csec_priv->dev,cipher_class1->key_addr,cipher_class1->key_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class2->key_addr,cipher_class1->key_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class1->iv_addr,cipher_class1->iv_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class2->iv_addr,cipher_class2->iv_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class2->data_addr,cipher_class2->data_len,DMA_BIDIRECTIONAL);
	kfree(key1_in_raw);
	kfree(key2_in_raw);
	kfree(iv1_in_raw);
	kfree(iv2_in_raw);
	kfree(data_in_raw);
	
	kfree(snoop_data_0);
	kfree(snoop_data_1);
	
	if(done_op->rst!=0)
	{
		kfree(done_op);
		return -1;
	}else
	{
		kfree(done_op);
		return 0;
	}

}
int snoop_sp_newapi_speed_do(void *_mparm)
{
	struct parm *mparm = _mparm;
	struct csec_priv_t *csec_priv;
	struct ccore_cards_t *ccore_cards = get_ccore_cards();
	struct cipher_core  cipher[2],*cipher_class1,*cipher_class2;
	struct custom_result *done_op;
	u8 *key1_in,*key1_in_raw,*key2_in,*key2_in_raw,*iv1_in,*iv1_in_raw,*iv2_in,*iv2_in_raw,*data_in,*data_in_raw;
	u8 data_tmp[4];

	int i,ret;

	int headlen = 16;

	cipher_class1 = cipher;
	cipher_class2 = &(cipher[1]);
	
	cipher_class1->key_len = mkeysize;
	cipher_class1->data_len = mdatasize;
	cipher_class1->iv_len = mivsize;
	cipher_class1->alg = malg;
	cipher_class1->type = mtype;
	cipher_class1->as = INITFINAL;
	cipher_class1->opt = mopt;

	cipher_class2->key_len = mkeysize2;
	cipher_class2->data_len = mdatasize+headlen;
	cipher_class2->iv_len = mivsize2;
	cipher_class2->alg = malg2;
	cipher_class2->type = mtype2;
	cipher_class2->as = INITFINAL;
	cipher_class2->opt = mopt2;
	
	key1_in_raw =(u8 *) kmalloc(cipher_class1->key_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	key1_in =(u8 *) (((size_t)key1_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	key2_in_raw =(u8 *) kmalloc(cipher_class2->key_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	key2_in =(u8 *) (((size_t)key2_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	iv1_in_raw =(u8 *) kmalloc(cipher_class1->iv_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	iv1_in = (u8 *) (((size_t)iv1_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	iv2_in_raw =(u8 *) kmalloc(cipher_class2->iv_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	iv2_in = (u8 *) (((size_t)iv2_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));

	data_in_raw =(u8 *) kmalloc(cipher_class2->data_len+DATA_MARGIN,GFP_KERNEL|SYS_DMA);
	data_in = (u8 *) (((size_t)data_in_raw+DATA_MARGIN)&(~(DATA_MARGIN-1)));
	
	done_op = kzalloc(sizeof(struct custom_result),GFP_KERNEL);
	
	csec_priv = cards_enqueue_pre(ccore_cards);

	memset(iv2_in,0,cipher_class2->iv_len);
	memcpy(data_tmp,data_in+cipher_class2->data_len-4,4);

	cipher_class1->key_addr = dma_map_single(csec_priv->dev,(void *)key1_in,cipher_class1->key_len, DMA_BIDIRECTIONAL);
	cipher_class2->key_addr = dma_map_single(csec_priv->dev,(void *)key2_in,cipher_class2->key_len, DMA_BIDIRECTIONAL);
	cipher_class1->iv_addr = dma_map_single(csec_priv->dev,(void *)iv1_in,cipher_class1->iv_len, DMA_BIDIRECTIONAL);
	cipher_class2->iv_addr = dma_map_single(csec_priv->dev,(void *)iv2_in,cipher_class2->iv_len, DMA_BIDIRECTIONAL);
	cipher_class2->data_addr = dma_map_single(csec_priv->dev,(void *)data_in,cipher_class2->data_len, DMA_BIDIRECTIONAL);
	cipher_class1->data_addr = cipher_class2->data_addr + headlen;

	wait_for_completion(&(mparm->w));
	
	//printk(KERN_ERR "2 mparm is %llx\n",mparm);
	for(i=0;i<DO_NUM;i++){

		new_interface_do(csec_priv,cipher,ACLASS_SNOOP_SP,done_op);

		do{
			dma_sync_single_for_cpu(csec_priv->dev,cipher_class2->iv_addr,cipher_class2->iv_len,DMA_BIDIRECTIONAL);
			ret = memcmp_with0(iv2_in,4);
			dma_sync_single_for_cpu(csec_priv->dev,cipher_class2->data_addr,cipher_class2->data_len,DMA_BIDIRECTIONAL);
			ret = (ret && memcmp(data_tmp,data_in+cipher_class2->data_len-4,4));
		}while(!ret);
		memset(done_op,0,sizeof(struct custom_result));
		memset(iv2_in,0,cipher_class2->iv_len);
		memcpy(data_tmp,data_in+cipher_class2->data_len-4,4);
		
	}
	//printk(KERN_ERR "3 mparm is %llx\n",mparm);
	complete(&(mparm->c));
	
	dma_unmap_single(csec_priv->dev,cipher_class1->key_addr,cipher_class1->key_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class2->key_addr,cipher_class1->key_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class1->iv_addr,cipher_class1->iv_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class2->iv_addr,cipher_class2->iv_len,DMA_BIDIRECTIONAL);
	dma_unmap_single(csec_priv->dev,cipher_class2->data_addr,cipher_class2->data_len,DMA_BIDIRECTIONAL);
	
	kfree(key1_in_raw);
	kfree(key2_in_raw);
	kfree(iv1_in_raw);
	kfree(iv2_in_raw);
	kfree(data_in_raw);
	
	if(done_op->rst!=0)
	{
		kfree(done_op);
		return -1;
	}else
	{
		kfree(done_op);
		return 0;
	}

}

